﻿using Protocol.Code;
using Script.Framework;
using Script.Scene;
using Script.Scene.Msg;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI._1.Main
{
    public class MatchPanel : UIBase
    {
        private Button btnMatch;
        private Image imgBg;
        private Text txtDsg;
        private Button btnCancel;
        private Button btnEnter;

        private SocketMsg socketMsg;

        private void Awake()
        {
            Bind(UIEvent.SHOW_ENTER_ROOM_BUTTON);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.SHOW_ENTER_ROOM_BUTTON:
                    btnEnter.gameObject.SetActive(true);
                    break;
            }
        }

        // Start is called before the first frame update
        void Start()
        {
            initVariable();

            initListener();

            initView();
        }

        private void initView()
        {
            // 初始状态
            SetObjectActive(false);
            btnEnter.gameObject.SetActive(false);
        }

        private void initListener()
        {
            // 注册事件
            btnMatch.onClick.AddListener(MatchClick);
            btnCancel.onClick.AddListener(cancelClick);
            btnEnter.onClick.AddListener(EnterClick);
        }

        private void initVariable()
        {
            // 获取对象
            btnCancel = transform.Find("Btn_Close").GetComponent<Button>();
            btnMatch = transform.Find("Btn_Match").GetComponent<Button>();
            btnEnter = transform.Find("Btn_Enter").GetComponent<Button>();
            txtDsg = transform.Find("txtDsg").GetComponent<Text>();
            imgBg = transform.Find("BgHuman").GetComponent<Image>();

            // 创建对象
            socketMsg = new SocketMsg();
        }

        private void EnterClick()
        {
            Dispatch(AreaCode.SCENE, SceneEvent.LOAD_SCENE, new LoadSceneMsg(2, null));
        }

        // Update is called once per frame
        void Update()
        {
            if (txtDsg.gameObject.activeInHierarchy == false)
            {
                return;
            }


            timer += Time.deltaTime;
            if (timer > intervalTime)
            {
                Animation();
                timer = 0f;
            }
        }

        private void MatchClick()
        {
            // 向服务器发送匹配的请求
            // 可以这么做
            // 保存自身的账号id的话，发消息的时候
            // 把自身的id发给服务器
            socketMsg.Change(OpCode.MATCH, MatchCode.ENTER_CREQ, null);
            Dispatch(AreaCode.NET, 0, socketMsg);


            SetObjectActive(true);

            // 隐藏匹配按钮防止多次请求逻辑错误
            btnMatch.interactable = false;
        }

        /// <summary>
        /// 控制点击匹配按钮之后的显示的物体
        /// </summary>
        private void SetObjectActive(bool active)
        {
            imgBg.gameObject.SetActive(active);
            txtDsg.gameObject.SetActive(active);
            btnCancel.gameObject.SetActive(active);
        }

        private string defaultText = "正在寻找房间";
        private int dotCount;
        private float intervalTime = 1f;
        private float timer;

        /// <summary>
        /// 为提示文字做的动画
        /// </summary>
        private void Animation()
        {
            defaultText = "正在寻找房间";
            dotCount++;
            if (dotCount > 3)
            {
                dotCount = 1;
            }

            //Debug.Log(dotCount);

            for (int i = 0; i < dotCount; i++)
            {
                defaultText += "。";
            }

            txtDsg.text = defaultText;
        }

        private void cancelClick()
        {
            // 向服务器离开匹配的请求
            socketMsg.Change(OpCode.MATCH, MatchCode.LEAVE_CREQ, null);
            Dispatch(AreaCode.NET, 0, socketMsg);

            SetObjectActive(false);

            // 可点击了
            btnMatch.interactable = true;
        }

        public override void OnDestroy()
        {
            base.OnDestroy();

            btnMatch.onClick.RemoveListener(MatchClick);
            btnCancel.onClick.RemoveListener(cancelClick);
            btnEnter.onClick.RemoveListener(EnterClick);
        }
    }
}